
CROSS_COMPILE = arm-openwrt-linux-muslgnueabi-
AS		= $(CROSS_COMPILE)as
LD		= $(CROSS_COMPILE)ld
CC		= $(CROSS_COMPILE)gcc
CPP		= $(CC) -E
AR		= $(CROSS_COMPILE)ar
NM		= $(CROSS_COMPILE)nm

STRIP		= $(CROSS_COMPILE)strip
OBJCOPY		= $(CROSS_COMPILE)objcopy
OBJDUMP		= $(CROSS_COMPILE)objdump

export AS LD CC CPP AR NM
export STRIP OBJCOPY OBJDUMP

CFLAGS := -Wall -g
CFLAGS += -I $(shell pwd)/include

LDFLAGS := -lsqlite3 -lm -lpthread -dl -lmodbus

export CFLAGS LDFLAGS

TOPDIR := $(shell pwd)
export TOPDIR

TARGET := pcs_upper

src=$(wildcard *.c)
obj=$(patsubst %.c,%.o,$(src))

obj-y += $(obj)

SRC_FOLDER := $(shell find $(TOPDIR) -maxdepth 1 -type d)
BASE_SRC_FOLDER := $(basename $(patsubst $(TOPDIR)/%, %/, $(SRC_FOLDER)))
#列出需要排除的文件夹
BASE_SRC_FOLDER := $(filter-out $(TOPDIR), $(BASE_SRC_FOLDER))
BASE_SRC_FOLDER := $(filter-out lib, $(BASE_SRC_FOLDER))
BASE_SRC_FOLDER := $(filter-out obj, $(BASE_SRC_FOLDER))
BASE_SRC_FOLDER := $(filter-out .vscode/, $(BASE_SRC_FOLDER))
BASE_SRC_FOLDER := $(filter-out include/, $(BASE_SRC_FOLDER))
#BASE_SRC_FOLDER := $(filter-out modbus/, $(BASE_SRC_FOLDER))

obj-y += $(BASE_SRC_FOLDER)

all : start_build $(TARGET)
	@echo $(TARGET) has been built!

start_build:
	make -C ./ -f $(TOPDIR)/Makefile.build

$(TARGET) : start_build
	$(CC) -o $(TARGET) built-in.o $(LDFLAGS)

clean:
	rm -f $(shell find -name "*.o")
	rm -f $(TARGET)

distclean:
	rm -f $(shell find -name "*.o")
	rm -f $(shell find -name "*.d")
	rm -f $(TARGET)
	
